﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Data;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    /// 
    public partial class QVWVK02 : DocumentBase
    {

        #region=============Properties=======================
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        string Mandant = "00";
        bool IsSetUp = false;
        YGRVK02LISTData vm;
        
        public DataSet setResult;
        ITXEXRPT ITXEXRPT;

        #region==========CollectionViewSource=================
        SETINTEXDB SetMaster
        {
            get
            {
                return vm.SetMaster;
            }
        }
        SETINTEXDB SetEnv
        {
            get
            {
                return vm.SetEnv;
            }
        }
        SETINTEXDB SetDetail
        {
            get
            {
                return vm.SetDetail;
            }
        }

        CollectionViewSource vwMaster
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwMaster");
            }
        }
        DataRow dwMaster
        {
            get
            {
                if (vwMaster.View == null)
                    return null;
                if (vwMaster.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwMaster.View.CurrentItem).Row;
            }
        }
        string ART
        {
            get
            {
                if (dwMaster == null)
                    return "";
                return dwMaster["VKPART"].ToString();

            }
        } 
        #endregion
        BindingListCollectionView cvMaster
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgMaster.ItemsSource);
            }
        }
        #endregion
        int total = 0;
        public QVWVK02()
        {
            InitializeComponent();
            this.Name = "销售统计";
           
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (IsLoaded)
                    return;
                IsLoaded = true;
                if (vm == null)
                    vm = DataContext as YGRVK02LISTData;
                SetEnv.Clear();
                SetEnv.Merge(vm.LoadEnv());
                IsLoaded = true;
                vwMaster.View.CurrentChanged += vwMaster_CurrentChanged;  
                
                if (app.User.Trim() == "ITXV")
                {
                    vm.DEND = "20151231"; 
                    btnQuery_Click(null, null);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            } 
        } 
        #region=========Chart=======
        public delegate object AsyncRunHandle(object p1 = null, object p2 = null, object p3 = null, object p4 = null, object p5 = null);
        private readonly TaskScheduler _sync = TaskScheduler.FromCurrentSynchronizationContext();
        public void AsyncRun(string TaskType, object p1 = null, object p2 = null, object p3 = null, object p4 = null, object p5 = null)
        {
            AsyncRunHandle fHandle = null;
            AsyncCallback callback = null;
            IAsyncResult aResult = null;
            WaitHandle waitHandle = null;
            switch (TaskType)
            {
                case "StartChart":
                    fHandle = new AsyncRunHandle(StartChart);
                    break;
            }
            callback = new AsyncCallback(this.AsyncCallbackImpl);
            aResult = fHandle.BeginInvoke(p1, p2, p3, p4, p5, callback, fHandle);
            waitHandle = aResult.AsyncWaitHandle;
            waitHandle.WaitOne();
        }
        public void AsyncCallbackImpl(IAsyncResult run)
        {
            try
            {
                AsyncRunHandle handle = run.AsyncState as AsyncRunHandle;
                object result = handle.EndInvoke(run);
                switch (handle.Method.Name)
                {
                    case "StartChart":
                        Task.Factory.StartNew(() => vm.SetUpChart(), new CancellationTokenSource().Token, TaskCreationOptions.None, _sync).Wait();
                        break;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private object StartChart(object P1 = null, object P2 = null, object P3 = null, object P4 = null, object P5 = null)
        { 
            return "开始产生图表完成";
        }
      
        private void btnRefreshChart_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            AsyncRun("StartChart", null, null, null);
        }
        #endregion
        void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            try
            {
                if (dwMaster == null)
                    return;
                string filter = "TXTIPIND ='" + dwMaster["VKPAUFKRS"].ToString() + dwMaster["VKPAUFLFD"].ToString() + "000IVDI                                             '";
                DataRow[] rows = SetMaster.ICC_TXTKON.Select(filter);
                if (rows.Length > 0)
                {
                    DataTable tb = rows.CopyToDataTable();
                    tb.TableName = "ICC_TXTKON";
                    SetDetail.ICC_TXTKON.Clear();
                    SetDetail.Merge(tb); 
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;
        }   
        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            DataSet SResult = new DataSet();
            try
            {
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    DataRow dwData = SetMaster.ICC_QUERYTB.NewRow();
                    dwData["MANDANT"] = app.Mandant;
                    SResult=INTEXDBC.QueryQVW_VKPOS(dwData, 1); 
                    SetMaster.Clear();
                    SetMaster.Merge(SResult);
                    //btnRefreshChart_Click(null,null);
                    if (vwMaster.View != null)
                    {
                        vwMaster.View.MoveCurrentToFirst();
                    }
                     
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            } 
        } 
       
    }
}
